<template>
  <div class="container">
    <!-- 面包屑 -->
    <XtxBreadcrumb />
    <!-- 筛选 -->
    <SubFilter @filter-change="changeSort"></SubFilter>
    <div class="goods-sort">
      <!-- 排序 -->
      <SubSort @filter-change="changeSort"></SubSort>
      <!-- 列表 -->
      <ul>
        <li v-for="item in list" :key="item.id">
          <GoodsItem :good="item" />
        </li>
      </ul>
      <!-- 加载 -->
      <XtxInfiniteLoading :loading="loading" :finished="finished" @infinite="getData" />
    </div>
  </div>
</template>

<script>
import SubFilter from './components/sub-filter.vue'
import SubSort from './components/sub-sort.vue'
import GoodsItem from './components/goods-item'
import { findSubcategoryList } from '@/api/category'
import { ref } from 'vue'
import { useRoute } from 'vue-router'
export default {
  name: 'SubCategory',
  components: { SubFilter, SubSort, GoodsItem },
  setup(props) {
    const list = ref([])
    const route = useRoute()
    const loading = ref(false)
    const finished = ref(false)
    let params = {
      categoryId: route.params.id,
      page: 1,
      pageSize: 20
    }
    const getData = async () => {
      loading.value = true
      const { result } = await findSubcategoryList(params)
      if (result.items.length) {
        list.value.push(...result.items)
        params.page++
      } else {
        finished.value = true
      }
      loading.value = false
    }
    const changeSort = (sortParams) => {
      params = { ...params, ...sortParams }
      params.page = 1
      list.value = []
      finished.value = false
      getData()
    }
    return {
      list,
      loading,
      finished,
      getData,
      changeSort
    }
  }
}
</script>

<style lang="scss" scoped>
.goods-sort {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    li {
      margin-bottom: 20px;
    }
  }
}
</style>